package cn.dcw.pethome.pet.controller;

import cn.dcw.pethome.basic.exception.MyException;
import cn.dcw.pethome.basic.util.AjaxResult;
import cn.dcw.pethome.basic.util.LoginContext;
import cn.dcw.pethome.basic.util.PageList;
import cn.dcw.pethome.pet.domain.Pet;
import cn.dcw.pethome.pet.domain.SearchMasterMsg;
import cn.dcw.pethome.pet.query.SearchMasterMsgQuery;
import cn.dcw.pethome.pet.service.ISearchMasterMsgService;
import cn.dcw.pethome.user.domain.LoginInfo;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.findAll();
    }
    //宠物寻主
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg masterMsg,HttpServletRequest request){
        try {
            seachMasterMsgService.add(masterMsg,request);
            return new AjaxResult(true, "操作成功");
        }catch (MyException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统错误");
        }
    }
    /**
     * 已处理消息
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        //获取登入用户信息
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);

        return seachMasterMsgService.processed(query,loginInfo.getId());
    }
    /**
     * 未处理消息
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        //获取登入用户信息
        LoginInfo loginInfo = LoginContext.getLoginInfo(request);
        if(loginInfo!=null){//客户登入的账号信息在redis中还没过期
            return seachMasterMsgService.pending(query,loginInfo.getId());
        }
        return null;
    }
    //查看用户发布消息


    /**
     * 处理寻主消息
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest Request){
        try {
            seachMasterMsgService.handle(pet,Request);
            return new AjaxResult(true, "操作成功");
        }catch (MyException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常");
        }

    }
}
